/**
 * 
 */
package com.bzbs.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bzbs.bean.Commodity;
import com.bzbs.mapper.CommodityMapper;
import com.bzbs.service.ICommodityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @ClassName: CommodityService
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author Lily
 * @date 2020年4月21日
 */
@Service
public class CommodityService implements ICommodityService {

	@Autowired
	private CommodityMapper commodityMapper;

	@Override
	public int addCommodity(Commodity commodity) {
		return commodityMapper.addCommodity(commodity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.bzbs.service.ICommodityService#queryCommodities(java.lang.Integer,
	 * java.lang.Integer)
	 */
	@Override
	public PageInfo<Commodity> queryCommodities(Integer pageNum, Integer pageSize, HttpServletRequest request) {
		if (pageNum == null || pageSize == null) {
			pageNum = 1;
			pageSize = 8;
		}

		PageHelper.startPage(pageNum, pageSize);
		List<Commodity> list = commodityMapper.queryCommodities();
		PageInfo<Commodity> pageInfo = new PageInfo<>(list);
/*
		for (Commodity commodity : list) {
			String imgPath = request.getServletContext().getRealPath("shoponline/img");

			File file = new File(imgPath, commodity.getImgName());
			// 假如文件不存在，则在写一次
			if (!file.exists()) {
				try {
					FileUtils.writeByteArrayToFile(file, commodity.getImgData());
				} catch (IllegalStateException | IOException e) {
					e.printStackTrace();
				}
			}
		}*/

		return pageInfo;
	}

	/* (non-Javadoc)
	 * @see com.bzbs.service.ICommodityService#queryCommodity(int)
	 */
	@Override
	public Commodity queryCommodity(int commId) {
		return commodityMapper.queryCommodity(commId);
	}

	/* 
	 * 更新单个商品
	 */
	@Override
	public int updateCommodity(Commodity commodity) {
		return commodityMapper.updateCommodity(commodity);
	}



}
